﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "Rate" here, you must also update the reference to "Rate" in App.config.
    public partial class MainService : IRate
    {
        public int Rate_Using(int RateID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Rate_ID", RateID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Rate_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        
        public DataSet Rate_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public RateInfo Rate_GetInfo(int RateID)
        {
            RateInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Rate_ID", RateID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new RateInfo();
                    retVal.ID = Convert.ToInt32(dr["Rate_ID"]);
                    retVal.Month = Convert.ToDateTime(dr["Rate_Month"]);
                    retVal.Social = Convert.ToDecimal(dr["Rate_Social"]);
                    retVal.Medical = Convert.ToDecimal(dr["Rate_Medical"]);
                    retVal.Unemployed = Convert.ToDecimal(dr["Rate_Unemployed"]);
                    retVal.Remain = Convert.ToDecimal(dr["Rate_Remain"]);
                    retVal.Discription = dr["Rate_Description"].ToString();


                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public RateInfo Rate_GetInfoByDate(DateTime _DateVal)
        {
            RateInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_GetByDate", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@DateVal", _DateVal);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new RateInfo();
                    retVal.ID = Convert.ToInt32(dr["Rate_ID"]);
                    retVal.Month = Convert.ToDateTime(dr["Rate_Month"]);
                    retVal.Social = Convert.ToDecimal(dr["Rate_Social"]);
                    retVal.Medical = Convert.ToDecimal(dr["Rate_Medical"]);
                    retVal.Unemployed = Convert.ToDecimal(dr["Rate_Unemployed"]);
                    retVal.Remain = Convert.ToDecimal(dr["Rate_Remain"]);
                    retVal.Discription = dr["Rate_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public RateInfo Rate_GetMax()
        {
            RateInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_GetMax", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new RateInfo();
                    retVal.ID = Convert.ToInt32(dr["Rate_ID"]);
                    retVal.Month = Convert.ToDateTime(dr["Rate_Month"]);
                    retVal.Social = Convert.ToDecimal(dr["Rate_Social"]);
                    retVal.Medical = Convert.ToDecimal(dr["Rate_Medical"]);
                    retVal.Unemployed = Convert.ToDecimal(dr["Rate_Unemployed"]);
                    retVal.Remain = Convert.ToDecimal(dr["Rate_Remain"]);
                    retVal.Discription = dr["Rate_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Rate_Insert(RateInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Rate_Month", eInfo.Month);
            dbCmd.Parameters.AddWithValue("@Rate_Social", eInfo.Social);
            dbCmd.Parameters.AddWithValue("@Rate_Medical", eInfo.Medical);
            dbCmd.Parameters.AddWithValue("@Rate_Unemployed", eInfo.Unemployed);
            dbCmd.Parameters.AddWithValue("@Rate_Remain", eInfo.Remain);
            dbCmd.Parameters.AddWithValue("@Rate_Description", eInfo.Discription);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Rate_Update(RateInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Rate_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Rate_Month", eInfo.Month);
            dbCmd.Parameters.AddWithValue("@Rate_Social", eInfo.Social);
            dbCmd.Parameters.AddWithValue("@Rate_Medical", eInfo.Medical);
            dbCmd.Parameters.AddWithValue("@Rate_Unemployed", eInfo.Unemployed);
            dbCmd.Parameters.AddWithValue("@Rate_Remain", eInfo.Remain);
            dbCmd.Parameters.AddWithValue("@Rate_Description", eInfo.Discription);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Rate_CheckDate(DateTime _FromDate, DateTime _ToDate)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_CheckDate", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@FromDate", _FromDate);
            dbCmd.Parameters.AddWithValue("@ToDate", _ToDate);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Rate_Delete(int RateID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Rate_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Rate_ID", RateID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
